<?php
namespace frontend\widgets;

use frontend\models\Page;
use yii\bootstrap\Widget;
use yii\behaviors\CacheableWidgetBehavior;

class MainMenu extends Widget
{
    public $nodeID;

    private $node;
    private $childrenList;

    public function behaviors()
    {
        return [
            [
                'class' => CacheableWidgetBehavior::className(),
                'cacheDuration' => 3600,
            ],
        ];
    }
    
    public function init()
    {
        parent::init();

        $this->node = Page::find()
            ->where(['id' => (int)$this->nodeID])
            ->andWhere(['is_draft' => Page::PUBLISHED])
            ->select('id, path, depth, sort, is_module')
            ->one();
        $flatChildrenList = $this->node
            ->getDescendants(2)
            ->where(['is_draft' => Page::PUBLISHED])
            ->select('id, title, alias, path, depth, is_module')
            ->asArray()
            ->all();
        $this->childrenList = $this->populateTree($flatChildrenList);
    }

    public function run()
    {   
     
        foreach ($this->childrenList as $key => $pageItem) 
        {
            echo '<li class="dropdown">';
            echo '<a class="dropbtn" href="/' . $pageItem['alias'] . '">' . $pageItem['title'] . '</a>';
            if(!empty($pageItem['children']))
            {
                echo '<div class="dropdown-content">';
                foreach ($pageItem['children'] as $key => $childrenPage) 
                {
                    if($childrenPage['is_module'] == Page::IS_STATIC_PAGE)
                    {
                        echo '<a href="/' . $pageItem['alias'] . '/' . $childrenPage['alias'] . '">' . $childrenPage['title'] . '</a>';
                    }
                    else 
                    {
                        echo '<a href="/' . $childrenPage['alias'] . '">' . $childrenPage['title'] . '</a>';
                    }
                }
                echo '</div>';
            }
            echo '</li>';
        }
    }

    private function populateTree($flatList)
    {
        $tree = array();
        
        foreach ($flatList as $key => &$item) 
        {
            $item['children'] = array();
            $item['path'] = explode('/', $item['path']);

            if($item['depth'] == 1)
            {
                array_push($tree, $item);
                unset($flatList[$key]);
            }
        }

        $flatList = array_values($flatList);

        foreach ($tree as &$treeItem) 
        {
            foreach ($flatList as $flatItem) 
            {  
                // if($treeItem['id'] == $flatItem['path'][$treeItem['depth']] && $flatItem['depth'] == ($treeItem['depth'] + 1))
                // {
                //     array_push($treeItem['children'], $flatItem);
                // }
                if(isset($flatItem['path'][$treeItem['depth']]) && $flatItem['path'][$treeItem['depth']] == $treeItem['id'])
                {
                    array_push($treeItem['children'], $flatItem);
                }
            }    
        }
      
        return $tree;
    }

}
